package offer.tree;

public class MirrorTreeNode {

    public static TreeNode mirror(TreeNode pRoot) {
        if (pRoot == null) {
            return pRoot;
        }
        if (pRoot.left != null || pRoot.right != null) {
            TreeNode peer = pRoot.left;
            pRoot.left = pRoot.right;
            pRoot.right = peer;
            mirror(pRoot.left);
            mirror(pRoot.right);
        }
        return pRoot;
    }

    public static TreeNode mirrorTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        if (root.left != null || root.right != null) {
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
            mirror(root.left);
            mirror(root.right);
        }
        return root;
    }
}
